package com.kotlin.mvpdemo.net;

import com.kotlin.mvpdemo.net.factory.LiveDataCallAdapterFactory;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * author : fang.zhang
 * e-mail : initzf@126.com
 * date   : 2020/6/6
 * desc   :
 */
public class Api {

    private static ApiService mApiImpl;

    private Api() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(15, TimeUnit.SECONDS);
        builder.readTimeout(15, TimeUnit.SECONDS);
        builder.retryOnConnectionFailure(true);

        OkHttpClient okHttpClient = builder.build();

        Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
        retrofitBuilder.client(okHttpClient);
        retrofitBuilder.baseUrl(UrlConstant.BASE_URL);
        retrofitBuilder.addConverterFactory(GsonConverterFactory.create());
        retrofitBuilder.addCallAdapterFactory(new LiveDataCallAdapterFactory());
        Retrofit retrofit = retrofitBuilder.build();
        mApiImpl = retrofit.create(ApiService.class);
    }

    public static synchronized ApiService request() {
        return Handler.api.mApiImpl;
    }


    private static class Handler {
        public static Api api = new Api();
    }
}
